package com.yao.util;

import com.yao.util.RandomFour;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author 妖悲冥
 * @date 2021/5/23 0023   -    14:07
 */
@Component
public class SendMail {
    @Autowired
    JavaMailSenderImpl mailSender;

    public String send(String mail) {
        String code = RandomFour.random();
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            helper.setSubject("妖悲冥の邮箱验证码");
            helper.setText("<div id=\"mailContentContainer\" class=\"qmbox qm_con_body_content qqmail_webmail_only\" style=\"\">\n" +
                    "    <div style=\"background: #eee\">\n" +
                    "        <table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\n" +
                    "            <tbody>\n" +
                    "            <tr>\n" +
                    "                <td>\n" +
                    "                    <div style=\"background:#fff\">\n" +
                    "                        <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                    "                            <thead>\n" +
                    "                            <tr>\n" +
                    "                                <td valign=\"middle\"\n" +
                    "                                    style=\"padding-left:30px;background-color:#415A94;color:#fff;padding:20px 40px;font-size: 21px;\">\n" +
                    "                                    妖悲冥の毕设\n" +
                    "                                </td>\n" +
                    "                            </tr>\n" +
                    "                            </thead>\n" +
                    "                            <tbody>\n" +
                    "                            <tr style=\"padding:40px 40px 0 40px;display:table-cell\">\n" +
                    "                                <td style=\"font-size:24px;line-height:1.5;color:#000;margin-top:40px\">邮箱验证码</td>\n" +
                    "                            </tr>\n" +
                    "                            <tr>\n" +
                    "                                <td style=\"font-size:14px;color:#333;padding:24px 40px 0 40px\">\n" +
                    "                                    尊敬的用户您好！\n" +
                    "                                    <br>\n" +
                    "                                    <br>\n" +
                    "                                    您的验证码是：" + code + "，请在 5 分钟内进行验证。如果该验证码不为您本人申请，请无视。\n" +
                    "                                </td>\n" +
                    "                            </tr>\n" +
                    "                            <tr style=\"padding:40px;display:table-cell\">\n" +
                    "                            </tr>\n" +
                    "                            </tbody>\n" +
                    "                        </table>\n" +
                    "                    </div>\n" +
                    "                    <div>\n" +
                    "                        <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                    "                            <tbody>\n" +
                    "                            <tr>\n" +
                    "                                <td style=\"padding:20px 40px;font-size:12px;color:#999;line-height:20px;background:#f7f7f7\">\n" +
                    "                                    <span style=\"font-size:14px;color:#929292\" rel=\"noopener\"\n" +
                    "                                          target=\"_blank\">感谢您的参与</span></td>\n" +
                    "                            </tr>\n" +
                    "                            </tbody>\n" +
                    "                        </table>\n" +
                    "                    </div>\n" +
                    "                </td>\n" +
                    "            </tr>\n" +
                    "            </tbody>\n" +
                    "        </table>\n" +
                    "    </div>\n" +
                    "</div>", true);

            helper.setTo(mail);
            helper.setFrom("1746858316@qq.com");
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            System.out.println(e.toString());
        }
        return code;
    }
}
